package paperfile

import (
	"context"

	"paper-pro/app/paperfile/api/internal/svc"
	"paper-pro/app/paperfile/api/internal/types"
	"paper-pro/pkg/ctxdata"
	"paper-pro/pkg/rpc/paperfile/pb/paperfile"

	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type ListMetasLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// list paper file metas
func NewListMetasLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListMetasLogic {
	return &ListMetasLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListMetasLogic) ListMetas(req *types.ListMetasReq) (resp *types.ListMetasResp, err error) {
	userId, err := ctxdata.GetUidFromCtx(l.ctx)
	if err != nil {
		return nil, errors.Wrapf(err, "List file metas: get UserId error: %v", err)
	}

	rpcResp, err := l.svcCtx.PaperfileRpc.ListMetas(l.ctx, &paperfile.ListMetasReq{
		UserId:      userId,
		ParseStatus: req.ParseStatus,
	})
	if err != nil {
		return nil, errors.Wrapf(err, "List file metas: PaperfileRpc ListMetas error: %v", err)
	}

	resp = &types.ListMetasResp{
		Metas: make([]types.PaperFileMeta, len(rpcResp.Metas)),
	}
	for i, meta := range rpcResp.Metas {
		copier.Copy(&resp.Metas[i], meta)
	}

	return
}
